<?php
//登录
function dologin(){
	$username=addslashes($_POST['username']);
	$password=md5($_POST['password']);
	$code=$_POST['code'];
	$code_s=$_SESSION['verify'];
	if($code==$code_s){
		$sql="select * from admin where username='{$username}' and password='{$password}'";
		$row=fetchOne($sql);
		if($row){
			$rUsername=$row['username'];
			$rId=$row['id'];
			//设置cookie
			setcookie("adminname",$rUsername,time()+7*24*3600);
			setcookie("adminId",$rId,time()+7*24*3600);
			//设置session
			$_SESSION['adminname']=$rUsername;
			$_SESSION['adminId']=$rId;
			$result=array("msg"=>"登录成功",'success' => true);
		}else{
			$result=array("msg"=>"用户名密码错误",'success' => false);
		}
	}else{
		$result=array("msg"=>"验证码错误",'success' => false);
	}
	echo json_encode($result);
}
//验证是否已经登陆
function checkLogin(){
	if($_SESSION['adminId']=='' && $_COOKIE['adminId']==''){
		header("location:".SURL."/admin/views/login.php");
	}
}
//退出登录
function loginOut(){
	$_SESSION=array();
	//删除cookie
	if(isset($_COOKIE[session_name()])){
		setcookie(session_name(),"",time()-1);
	}
	if(isset($_COOKIE['adminId'])){
		setcookie('adminId',"",time()-1);
	}
	if(isset($_COOKIE['adminname'])){
		setcookie('adminname',"",time()-1);
	}
	//销毁
	session_destroy();
	$result=array('success' => true);
	echo json_encode($result);
	//header("location:../admin/views/login.php");
}
//修改密码
function editPassword(){
	$id=$_SESSION['adminId'];
	$oPd=md5($_POST['oldPassword']);
	$sql="select * from admin where id=$id and password='{$oPd}'";
	$row=fetchOne($sql);
	if($row){
		$arr['password']=md5($_POST['password']);
		if(update("admin",$arr,"id=$id")){
			$msg="密码修改成功";
		}else{
			$msg="密码修改失败";
		}
	}else{
		$msg="原密码错误";
	}
	return $msg;
}
//信息配置
function config(){
	//var_dump($_POST);
	$arr=$_POST;
	if(!empty($_FILES['logo']['name'])){
		$info=uploadFile($_FILES['logo'],"../admin/uploadimg/config");
		$arr['logo']=$info['name'];
	}
	if(!empty($_FILES['weixin']['name'])){
		$info=uploadFile($_FILES['weixin'],"../admin/uploadimg/config");
		$arr['weixin']=$info['name'];
	}
	if(!empty($_FILES['app']['name'])){
		$info=uploadFile($_FILES['app'],"../admin/uploadimg/config");
		$arr['app']=$info['name'];
	}
	if(update("config",$arr,"id=1")){
		$msg="保存成功";
	}else{
		$msg="保存失败";
	}
	return $msg;
}
//管理员列表
function managerList(){
	$pageSize=$_POST['pageSize'];
	$page=$_POST['curPage']?$_POST['curPage']:1;
	$startCount=($page-1)*$pageSize;
	$username=$_POST['username']?$_POST['username']:null;
	$departmentId=$_POST['departmentId']?$_POST['departmentId']:null;
	$where=$username?" and username like '%{$username}%'":null;
	$where.=$departmentId?" and departmentId='{$departmentId}'":null;
	$sql="select admin.id,username,realname,department.departmentName,admin.illustrate,state from admin,department where admin.departmentId = department.id{$where} LIMIT $startCount,$pageSize";
	$row=fetchAll($sql);
	if($row){
		$sqls="select * from admin where 1 {$where}";
		$rowNum=getResultNum($sqls);
		$arr=array("success"=>true,"totalRows"=>$rowNum,"curPage"=>$page,'data'=>$row);
	}else{
		$arr=array("success"=>true,"totalRows"=>0,"curPage"=>1,'data'=>[]);
	}
	echo json_encode($arr);
}
//添加管理员
function managerAdd(){
	$arr=$_POST;
	$arr['password']=md5($_POST['password']);
	if(insert("admin",$arr)){
		$msg="添加成功！";
	}else{
		$msg="添加失败！";
	}
	return $msg;
}
//编辑管理员
function managerEdit(){
	$id=$_REQUEST['id'];
	$arr['username']=$_POST['username'];
	if($_POST['password']!=''){
		$arr['password']=md5($_POST['password']);
	}
	$arr['realname']=$_POST['realname'];
	$arr['departmentId']=$_POST['departmentId'];
	$arr['illustrate']=$_POST['illustrate'];
	$arr['state']=$_POST['state'];
	if(update("admin",$arr,"id=$id")){
		$msg="修改成功！";
	}else{
		$msg="修改失败！";
	}
	return $msg;
}
//删除管理员
function managerDel(){
	$id=$_POST['id'];
	if(delete("admin","id=$id")){
		$result = array('Success' => true, 'Msg' => '删除成功');
	}else{
		$result = array('Success' => false, 'Msg' => '删除失败');
	}
	echo json_encode($result);
}
//删除多个管理员
function managerDels(){
	$ids=implode(',',$_POST['ids']);
	if(delete("admin","id in($ids)")){
		$result = array('Success' => true, 'Msg' => '删除成功');
	}else{
		$result = array('Success' => false, 'Msg' => '删除失败');
	}
	echo json_encode($result);
}
//部门列表
function departmentList(){
	$pageSize=$_POST['pageSize'];
	$page=$_POST['curPage']?$_POST['curPage']:1;
	$startCount=($page-1)*$pageSize;
	$sql="select id,departmentName,illustrate from department LIMIT $startCount,$pageSize";
	$row=fetchAll($sql);
	$sqls="select * from department";
	$rowNum=getResultNum($sqls);
	$arr=array("success"=>true,"totalRows"=>$rowNum,"curPage"=>$page,'data'=>$row);
	echo json_encode($arr);
}
//添加部门
function departmentAdd(){
	$arr=$_POST;
	if(insert("department",$arr)){
		$msg="添加成功！";
	}else{
		$msg="添加失败！";
	}
	return $msg;
}
//编辑部门
function departmentEdit(){
	$id=$_REQUEST['id'];
	$arr=$_POST;
	if(update("department",$arr,"id=$id")){
		$msg="修改成功！";
	}else{
		$msg="修改失败！";
	}
	return $msg;
}
//删除部门
function departmentDel(){
	$id=$_POST['id'];
	if(delete("department","id=$id")){
		$result = array('Success' => true, 'Msg' => '删除成功');
	}else{
		$result = array('Success' => false, 'Msg' => '删除失败');
	}
	echo json_encode($result);
}